//
//  BaseViewController.swift
//  Test
//
//  Created by ZWJY on 2021/9/18.
//

import UIKit
import SnapKit

class BaseViewController: UIViewController {
    
    ///自定义返回按钮
    private lazy var backBtn: UIButton = {
        let btn = UIButton.init(title: nil, image: UIImage(named: "media_player_previous_disabled"), titleColor: .white, font: UIFont.font14)
            .pg_addTarget(self, #selector(backAction))
        return btn
    }()
    
    /// 自定义导航条
    lazy var navigationBar: NavigationBar = {
        let bar = NavigationBar.init()
        bar.hasBottomLine = false
        bar.isTranslucent = false
        bar.type = .white
        return bar
    }()
    
    /// 自定义标题
    private lazy var titleLab: UILabel = {
        let lab = UILabel.init(text: nil, textColor: .black, font: .boldFont16)
            .pg_textAlignment(.center)
        return lab
    }()
    
    ///  自定义占位图(通用)
    lazy var emptyView: EmptyView = {
        let view = EmptyView.init()
        view.isHidden = true
        view.backgroundColor = UIColor.clear
        return view
    }()
    
    ///  通过导航条更新状态栏颜色
    /// 状态栏
    var barStyle: UIStatusBarStyle = .default
    
    /// 状态栏颜色控制
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return barStyle
    }
    
    /// 添加标题相关属性
    var titleString: String? {
        didSet{
            guard let title = titleString else { return }
            titleLab.text = title
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configNav()
        addViews()
        addContraints()
        view.backgroundColor = .white
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        view.bringSubview(toFront: navigationBar)
    }
    
    deinit {
        print("✅控制器：\(self)释放了✅")
    }
}

// MARK: - UI
extension BaseViewController {
    
    private func configNav() {
        
        if navigationBar.type == .white {
            backBtn.setImage(UIImage(named: "back_black"), for: .normal)
        } else {
            backBtn.setImage(UIImage(named: "back_white"), for: .normal)
        }
        navigationBar.titleView = titleLab
        navigationBar.leftItem = backBtn
        let count = self.navigationController?.childViewControllers.count ?? 0
        backBtn.isHidden = count == 1
    }
    
    private func addViews() {
        view.addSubview(navigationBar)
        view.addSubview(emptyView)
        view.bringSubview(toFront: navigationBar)
    }
    
    private func addContraints() {
        navigationBar.snp.makeConstraints { make  in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(UIDevice.navigtionHeight)
        }
        
        emptyView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

// MARK: - action
extension BaseViewController {
    
    /// 子类重写返回方法
    @objc public func backAction() {
        guard let vcs = navigationController?.viewControllers, !vcs.isEmpty else {
            dismiss(animated: true, completion: nil)
            return
        }
        navigationController?.popViewController(animated: true)
    }
}

// MARK: - 占位图
extension BaseViewController {
    
    /// 空集合展示
    public func showEmptyView(empty: Bool) {
        emptyView.isHidden = !empty
        if empty {
            self.view.bringSubview(toFront: emptyView)
        } else {
            self.view.sendSubview(toBack: emptyView)
        }
        emptyView.configEmptyView(emptyDesp:  "暂无内容",
                                  emptyIcon: UIImage(named: "empty_data"),
                                  target: nil,
                                  action: nil)
    }
    
    /// 接口报错展示
    public func showErrorEmptyView(emptyDesp: String?) {
        emptyView.isHidden = false
        self.view.bringSubview(toFront: emptyView)
        emptyView.configEmptyView(emptyDesp:  emptyDesp,
                                  emptyIcon: UIImage(named: "empty_data"),
                                  target: nil,
                                  action: nil)
    }
    
    /// 网络请求报错
    public func showNetEmptyView(emptyDesp: String?,
                                 action: Selector? = nil) {
        emptyView.isHidden = false
        self.view.bringSubview(toFront: emptyView)
        emptyView.configEmptyView(emptyDesp:  emptyDesp,
                                  emptyIcon: UIImage(named: "empty_data"),
                                  target: self,
                                  action: action)
    }
}
